(function ($) {
    var YH = window.YH = window.YH || {};
    $.extend(YH, {
        template: _.memoize(function (id) {
            var compiled,
                options = {
                    variable: 'data'
                };

            return function (data) {
                compiled = compiled || _.template($('#tmpl-' + id).html(), null, options);
                return compiled(data);
            };
        }),

        getCsrfToken: function() {
            var csrf = {};
            csrf[$('meta[name=csrf-param]').attr('content')] = $('meta[name=csrf-token]').attr('content');
            return csrf;
        },

        getModal: function() {
            var self = this;
            if (!this.model) {
                this.modal = $('<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="YH-modal-label" aria-hidden="true">' +
                    '<div class="modal-dialog">' +
                    '<div class="modal-content">' +
                    '<div class="modal-header">'+
                    '<button type="button" class="close" data-dismiss="modal">' +
                    '<span aria-hidden="true">&times;</span>' +
                    '<span class="sr-only">关闭</span>' +
                    '</button>' +
                    '<h4 class="modal-title" id="YH-modal-label"></h4>' +
                    '</div>' +
                    '<div class="modal-body"></div>' +
                    '<div class="modal-footer"></div>' +
                    '</div></div></div>');
                this.modal.on('hidden.bs.modal', function() {
                    $(this).remove();
                    delete self.modal;
                });
                $('body').append(this.modal)
            }
            return this.modal;
        },

        alert: function(content, title) {
            var modal = this.getModal();
            modal.find('.modal-body').html(content);
            if (title) {
                modal.find('.modal-title').html(title);
            }
            modal.find('.modal-footer').html('<button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>');
            modal.modal('show');
            return modal;
        },

        confirm: function(content, title, callback) {
            var modal = this.getModal(), confirmBtn;
            if (arguments.length == 2) {
                if ($.isFunction(title)) {
                    callback = title;
                    title = undefined;
                }
            }

            modal.find('.modal-body').html(content);
            if (title) {
                modal.find('.modal-title').html(title);
            }

            confirmBtn = $('<button type="button" class="btn btn-primary">确定</button>');

            if (callback) {
                confirmBtn.one('click', callback);
                modal.on('hidden.bs.modal', function() {
                    confirmBtn.off('click');
                });
            }

            modal.find('.modal-footer')
                .html('<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>')
                .append(confirmBtn);

            modal.modal('show');
            return modal;
        }
    });
})(jQuery);